package cn.ljy.mes.service.impl;

import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyConfirmException;
import cn.ljy.common.exception.model.MyServiceException;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.mes.dao.mapper.MesCarModelConfigMapper;
import cn.ljy.mes.dao.mapper.MesCarModelMapper;
import cn.ljy.mes.dao.mapper.MesCarTechnologyMapper;
import cn.ljy.mes.model.MesCarModel;
import cn.ljy.mes.model.MesCarModelConfig;
import cn.ljy.mes.model.query.MesCarModelConfigQuery;
import cn.ljy.mes.model.query.MesCarModelQuery;
import cn.ljy.mes.model.query.MesCarTechnologyQuery;
import cn.ljy.mes.service.MesCarModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IMesCarModelService extends IBaseService<MesCarModel, MesCarModelQuery> implements MesCarModelService {
    @Autowired
    private MesCarModelMapper mapper;
    @Autowired
    private MesCarTechnologyMapper mesCarTechnologyMapper;
    @Autowired
    private MesCarModelConfigMapper carModelConfigMapper;

    @Override
    protected void enrichEntityFields(MesCarModel mesCarModel){
        if(mesCarModel!=null){
            //配置数量
            MesCarModelConfigQuery carModelConfigQuery=new MesCarModelConfigQuery();
            carModelConfigQuery.setCarModel(mesCarModel);
            mesCarModel.setCarModelConfigNumber(carModelConfigMapper.count(carModelConfigQuery));
            //工艺数量
//            MesCarTechnologyQuery carTechnologyQuery=new MesCarTechnologyQuery();
//            carTechnologyQuery.setCarModel(mesCarModel);
//            mesCarModel.setCarTechnologyNumber(mesCarTechnologyMapper.count(carTechnologyQuery));
        }
    }

    @Override
    protected void beforeSaveOrUpdate(MesCarModel mesCarModel) {
        if(mesCarModel.getCarBrand().getId()==null){
            throw new MyWebException("验证失败：品牌id不能为空");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        Map<String,String> errorMessageMap=new HashMap();
        for(Serializable id:ids){
            MesCarModel carModel=mapper.findByIdNoRelation(id);
            if(carModel!=null){
                //配置数量
                MesCarModelConfigQuery carModelConfigQuery=new MesCarModelConfigQuery();
                carModelConfigQuery.setCarModel(carModel);
                int carModelConfigNumber=carModelConfigMapper.count(carModelConfigQuery);
                if(carModelConfigNumber>0){
                    errorMessageMap.put(carModel.getName(),String.valueOf(carModelConfigNumber));
                }
            }
        }
        //确定是否抛出错误信息
        if(errorMessageMap.size()>0){
            throw new MyConfirmException("%s下存在%s个配置",errorMessageMap);
        }
    }


    @Override
    protected void beforeForceDelete(Serializable[] ids) {
        List<MesCarModelConfig> carModelConfigsOfAll=new ArrayList<>();//定义和获取所有配置信息，用于最后的删除
        for(Serializable id:ids) {
            MesCarModel carModel = mapper.findByIdNoRelation(id);
            if (carModel != null) {
                //1.先读取车型下的所有配置
                MesCarModelConfigQuery carModelConfigQuery=new MesCarModelConfigQuery();
                carModelConfigQuery.setCarModel(carModel);
                List<MesCarModelConfig> carModelConfigs = carModelConfigMapper.findAll(carModelConfigQuery);
                for(MesCarModelConfig carModelConfig:carModelConfigs){
                    //2.获取配置下的所有工艺数量，如果存在则不允许删除
                    MesCarTechnologyQuery carTechnologyQuery=new MesCarTechnologyQuery();
                    carTechnologyQuery.setCarModelConfig(carModelConfig);
                    int carTechnologyNumber=mesCarTechnologyMapper.count(carTechnologyQuery);
                    if(carTechnologyNumber>0){
                        throw new MyServiceException(String.format("删除失败：车型%s的配置%s已经拥有%d个工艺，请先移除这些工艺再执行删除",carModel.getName(),carModelConfig.getName(),carTechnologyNumber));
                    }
                }
                //添加配置信息集合
                carModelConfigsOfAll.addAll(carModelConfigs);
            }
        }
        //删除所有配置信息
        carModelConfigMapper.deleteAllIn(carModelConfigsOfAll);
    }
}
